<html>
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"/>
  <title>
   主题：运用import过程进行SAS数据导入完全实用教程  | 数螺 | NAUT IDEA
  </title>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet"/>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
  <style type="text/css">
   #xmain img {
                  max-width: 100%;
                  display: block;
                  margin-top: 10px;
                  margin-bottom: 10px;
                }

                #xmain p {
                    line-height:150%;
                    font-size: 16px;
                    margin-top: 20px;
                }

                #xmain h2 {
                    font-size: 24px;
                }

                #xmain h3 {
                    font-size: 20px;
                }

                #xmain h4 {
                    font-size: 18px;
                }


                .header {
	           background-color: #0099ff;
	           color: #ffffff;
	           margin-bottom: 20px;
	        }

	        .header p {
                  margin: 0px;
                  padding: 10px 0;
                  display: inline-block;  
                  vertical-align: middle;
                  font-size: 16px;
               }

               .header a {
                 color: white;
               }

              .header img {
                 height: 25px;
              }
  </style>
  <script src="http://cdn.bootcss.com/jquery/3.0.0/jquery.min.js">
  </script>
  <script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript">
   MathJax.Hub.Config({elements: ["bbpress-forums"]});
  </script>
  <script src="http://nautstatic-10007657.file.myqcloud.com/static/css/readability.min.js" type="text/javascript">
  </script>
  <script type="text/javascript">
   $(document).ready(function() {
                 var loc = document.location;
                 var uri = {
                  spec: "http://cos.name/cn/topic/16507/",
                  host: "http://cos.name",
                  prePath: "http://cos.name",
                  scheme: "http",
                  pathBase: "http://cos.name/"
                 };
    
                 var documentClone = document.cloneNode(true);
                 var article = new Readability(uri, documentClone).parse();
     
                 document.getElementById("xmain").innerHTML = article.content;
                });
  </script>
  <!-- 1466466497: Accept with keywords: (title(0.2):数据,实用教程,主题,过程,论坛, topn(0.1):RSS订阅,软件应用,本例,帖子,文件夹,文件名称,过程,文件,用户名,读取数据,文件名,主站,汇总,中都,源代码,数据,值阈,版主,数据表,总计,格式文件,实用教程,密码,代码,主题,程序,文件格式,论坛,文件类型,讨论区).-->
 </head>
 <body class="topic bbpress single single-topic postid-16507 single-author sidebar" onload="">
  <div class="header">
   <div class="container">
    <div class="row">
     <div class="col-xs-6 col-sm-6 text-left">
      <a href="/databee">
       <img src="http://nautidea-10007657.cos.myqcloud.com/logo_white.png"/>
      </a>
      <a href="/databee">
       <p>
        数螺
       </p>
      </a>
     </div>
     <div class="hidden-xs col-sm-6 text-right">
      <p>
       致力于数据科学的推广和知识传播
      </p>
     </div>
    </div>
   </div>
  </div>
  <div class="container text-center">
   <h1>
    主题：运用import过程进行SAS数据导入完全实用教程
   </h1>
  </div>
  <div class="container" id="xmain">
   <div class="hfeed site" id="page">
    <header class="site-header" id="masthead" role="banner">
     <div id="cos-logo">
      <a href="http://cos.name/cn">
       <img src="http://cos.name/cn/wp-content/themes/COS-forest/images/headers/cos-logo.png"/>
      </a>
     </div>
     <div class="navbar" id="navbar">
      <nav class="navigation main-navigation" id="site-navigation" role="navigation">
       <h3 class="menu-toggle">
        菜单
       </h3>
       <div class="menu-%e8%8f%9c%e5%8d%951-container">
        <ul class="nav-menu" id="menu-%e8%8f%9c%e5%8d%951">
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-407772" id="menu-item-407772">
          <a href="http://cos.name/cn/">
           论坛首页
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-407773" id="menu-item-407773">
          <a href="http://cos.name/cn/forums/">
           讨论区
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-407774" id="menu-item-407774">
          <a href="http://cos.name/cn/wp-login.php?action=register">
           注册
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-407819" id="menu-item-407819">
          <a href="http://cos.name/">
           主站
          </a>
         </li>
        </ul>
       </div>
      </nav>
      <!-- #site-navigation -->
     </div>
     <!-- #navbar -->
    </header>
    <!-- #masthead -->
    <div class="site-main" id="main">
     <div class="content-area" id="primary">
      <div class="site-content" id="content" role="main">
       <article class="post-16507 topic type-topic status-publish hentry" id="post-16507">
        <header class="entry-header">
         <h1 class="entry-title">
          运用import过程进行SAS数据导入完全实用教程
         </h1>
        </header>
        <!-- .entry-header -->
        <div class="entry-content">
         <div id="bbpress-forums">
          <div class="bbp-breadcrumb">
           <p>
            <a class="bbp-breadcrumb-home" href="http://cos.name/cn/">
             COS论坛 | 统计之都
            </a>
            <span class="bbp-breadcrumb-sep">
             ›
            </span>
            <a class="bbp-breadcrumb-root" href="http://cos.name/cn/forums/">
             讨论区
            </a>
            <span class="bbp-breadcrumb-sep">
             ›
            </span>
            <a class="bbp-breadcrumb-forum" href="http://cos.name/cn/forum/software/">
             软件应用
            </a>
            <span class="bbp-breadcrumb-sep">
             ›
            </span>
            <a class="bbp-breadcrumb-forum" href="http://cos.name/cn/forum/software/sas/">
             SAS
            </a>
            <span class="bbp-breadcrumb-sep">
             ›
            </span>
            <span class="bbp-breadcrumb-current">
             运用import过程进行SAS数据导入完全实用教程
            </span>
           </p>
          </div>
          <div class="bbp-template-notice info">
           <p class="bbp-topic-description">
            该主题包含 1 条回复，2个帖子，最后由
            <a class="bbp-author-avatar" href="http://cos.name/cn/profile/2172/" rel="nofollow" title="查看kaikaiok8的档案">
             <img src="http://sdn.geekzu.org/avatar/09bb0e1175bb28e69acfd177f66c5394?s=14&amp;d=monsterid&amp;r=g"/>
            </a>
            <a class="bbp-author-name" href="http://cos.name/cn/profile/2172/" rel="nofollow" title="查看kaikaiok8的档案">
             kaikaiok8
            </a>
            在
            <a href="http://cos.name/cn/topic/16507/#post-277542" title="回复：运用import过程进行SAS数据导入完全实用教程">
             6 年, 9 月 之前
            </a>
            更新。
           </p>
          </div>
          <div class="bbp-pagination">
           <div class="bbp-pagination-count">
            查看 2 个帖子 - 1 到 2（总计 2 个）
           </div>
           <div class="bbp-pagination-links">
           </div>
          </div>
          <ul class="forums bbp-replies" id="topic-16507-replies">
           <li class="bbp-header">
            <div class="bbp-reply-author">
             作者
            </div>
            <!-- .bbp-reply-author -->
            <div class="bbp-reply-content">
             帖子
            </div>
            <!-- .bbp-reply-content -->
           </li>
           <!-- .bbp-header -->
           <li class="bbp-body">
            <div class="bbp-reply-header" id="post-16507">
             <div class="bbp-meta">
              <span class="bbp-reply-post-date">
               2009年9月16日 上午9:49
              </span>
              <a class="bbp-reply-permalink" href="http://cos.name/cn/topic/16507/#post-16507">
               1 楼
              </a>
              <span class="bbp-admin-links">
              </span>
             </div>
             <!-- .bbp-meta -->
            </div>
            <!-- #post-16507 -->
            <div class="odd bbp-parent-forum-998 bbp-parent-topic-16507 bbp-reply-position-1 user-id-103519 topic-author post-16507 topic type-topic status-publish hentry">
             <div class="bbp-reply-author">
              <a class="bbp-author-avatar" href="http://cos.name/cn/profile/103519/" rel="nofollow" title="查看tangyh的档案">
               <img src="http://sdn.geekzu.org/avatar/b75e7dc14d36ea26ca18d521d8a83aff?s=80&amp;d=monsterid&amp;r=g"/>
              </a>
              <br/>
              <a class="bbp-author-name" href="http://cos.name/cn/profile/103519/" rel="nofollow" title="查看tangyh的档案">
               tangyh
              </a>
              <br/>
              <div class="bbp-author-role">
               版主
              </div>
             </div>
             <!-- .bbp-reply-author -->
             <div class="bbp-reply-content">
              <p>
               运用import过程进行SAS数据导入完全实用教程(2009-08-12 18:43:29)
               <br/>
               标签：sas import 数据导入 教育      分类：Proc过程
              </p>
              <p>
               运用import过程进行SAS数据导入完全实用教程
              </p>
              <p>
               转载请注明出处：
               <a class="d4pbbc-url" href="http://blog.sina.com.cn/s/blog_5d3b177c0100e7fa.html" rel="nofollow" target="_blank">
                http://blog.sina.com.cn/s/blog_5d3b177c0100e7fa.html
               </a>
              </p>
              <p>
               1 单个规范格式文件导入。
              </p>
              <p>
               对单个文件进行导入是我们遇到最多的情况，主要有以下几种：
              </p>
              <p>
               1.1 对指定分隔符（’|’，’ ’，’!’，’ab’等）数据的导入，这里以’!’为例delimiter='!'进行说明：
              </p>
              <p>
               data _null_;
              </p>
              <p>
               file 'c:\temp\pipefile.txt';
              </p>
              <p>
               put"X1!X2!X3!X4";
              </p>
              <p>
               put "11!22!.! ";
              </p>
              <p>
               put "111!.!333!apple";
              </p>
              <p>
               run;
              </p>
              <p>
               导入程序：
              </p>
              <p>
               proc import
              </p>
              <p>
               datafile='c:\temp\pipefile.txt'
              </p>
              <p>
               out=work.test
              </p>
              <p>
               dbms=dlm
              </p>
              <p>
               replace;
              </p>
              <p>
               delimiter='!';
              </p>
              <p>
               GUESSINGROWS=2000;
              </p>
              <p>
               DATAROW=2;
              </p>
              <p>
               getnames=yes;
              </p>
              <p>
               run;
              </p>
              <p>
               注意GUESSINGROWS的值阈为1 到 3276
              </p>
              <p>
               1.2 对CSV格式的数据进行导入：
              </p>
              <p>
               data _null_;
              </p>
              <p>
               file 'c:\temp\csvfile.csv';
              </p>
              <p>
               put "Fruit1,Fruit2,Fruit3,Fruit4";
              </p>
              <p>
               put "apple,banana,coconut,date";
              </p>
              <p>
               put "apricot,berry,crabapple,dewberry";
              </p>
              <p>
               run;
              </p>
              <p>
               导入程序：
              </p>
              <p>
               proc import
              </p>
              <p>
               datafile='c:\temp\csvfile.csv'
              </p>
              <p>
               out=work.fruit
              </p>
              <p>
               dbms=csv
              </p>
              <p>
               replace;
              </p>
              <p>
               run;
              </p>
              <p>
               1.3 对tab分隔数据的导入：
              </p>
              <p>
               data _null_;
              </p>
              <p>
               file 'c:\temp\tabfile.txt';
              </p>
              <p>
               put "cereal" "09"x "eggs" "09"x "bacon";
              </p>
              <p>
               put "muffin" "09"x "berries" "09"x "toast";
              </p>
              <p>
               run;
              </p>
              <p>
               proc import
              </p>
              <p>
               datafile='c:\temp\tabfile.txt'
              </p>
              <p>
               out=work.breakfast
              </p>
              <p>
               dbms=tab
              </p>
              <p>
               replace;
              </p>
              <p>
               getnames=no;
              </p>
              <p>
               run;
              </p>
              <p>
               1.4 对dbf数据库数据进行导入：
              </p>
              <p>
               proc import datafile="/myfiles/mydata.dbf"
              </p>
              <p>
               out=sasuser.mydata
              </p>
              <p>
               dbms=dbf
              </p>
              <p>
               replace;
              </p>
              <p>
               run;
              </p>
              <p>
               1.5对excel数据进行导入：
              </p>
              <p>
               PROC IMPORT OUT= hospital1
              </p>
              <p>
               DATAFILE= " C:\My Documents\Excel Files\Hospital1.xls "
              </p>
              <p>
               DBMS=EXCEL REPLACE;
              </p>
              <p>
               SHEET="Sheet1$";
              </p>
              <p>
               GETNAMES=YES;
              </p>
              <p>
               MIXED=NO;
              </p>
              <p>
               SCANTEXT=YES;
              </p>
              <p>
               USEDATE=YES;
              </p>
              <p>
               SCANTIME=YES;
              </p>
              <p>
               RUN;
              </p>
              <p>
               1.6对access数据进行导入：
              </p>
              <p>
               PROC IMPORT DBMS=ACCESS TABLE="customers" OUT=sasuser.cust;
              </p>
              <p>
               DATABASE="c:\demo\customers.mdb";
              </p>
              <p>
               UID="bob";
              </p>
              <p>
               PWD="cat";                                WGDB="c:\winnt\system32\system.mdb";
              </p>
              <p>
               RUN;
              </p>
              <p>
               proc print data=sasuser.cust;
              </p>
              <p>
               run;
              </p>
              <p>
               1.7 import过程步中，dbms选项汇总：
              </p>
              <p>
               Identifier
               <br/>
              </p>
              <p>
               Input Data Source
               <br/>
              </p>
              <p>
               Extension
               <br/>
              </p>
              <p>
               Host Availability
              </p>
              <p>
               ACCESS
               <br/>
              </p>
              <p>
               Microsoft Access 2000 or 2002 table
               <br/>
              </p>
              <p>
               .mdb
               <br/>
              </p>
              <p>
               Microsoft Windows *
              </p>
              <p>
               ACCESS97
               <br/>
              </p>
              <p>
               Microsoft Access 97 table
               <br/>
              </p>
              <p>
               .mdb
               <br/>
              </p>
              <p>
               Microsoft Windows *
              </p>
              <p>
               ACCESS2000
               <br/>
              </p>
              <p>
               Microsoft Access 2000 table
               <br/>
              </p>
              <p>
               .mdb
               <br/>
              </p>
              <p>
               Microsoft Windows *
              </p>
              <p>
               ACCESS2002
               <br/>
              </p>
              <p>
               Microsoft Access 2002 table
               <br/>
              </p>
              <p>
               .mdb
               <br/>
              </p>
              <p>
               Microsoft Windows *
              </p>
              <p>
               ACCESSCS
               <br/>
              </p>
              <p>
               Microsoft Access table
               <br/>
              </p>
              <p>
               .mdb
               <br/>
              </p>
              <p>
               UNIX
              </p>
              <p>
               CSV
               <br/>
              </p>
              <p>
               delimited file (comma-separated values)
               <br/>
              </p>
              <p>
               .csv
               <br/>
              </p>
              <p>
               OpenVMS Alpha, UNIX, Microsoft Windows
              </p>
              <p>
               DBF
               <br/>
              </p>
              <p>
               dBASE 5.0, IV, III+, and III files
               <br/>
              </p>
              <p>
               .dbf
               <br/>
              </p>
              <p>
               UNIX, Microsoft Windows
              </p>
              <p>
               DLM
               <br/>
              </p>
              <p>
               delimited file (default delimiter is a blank)
               <br/>
              </p>
              <p>
               .*
               <br/>
              </p>
              <p>
               OpenVMS Alpha, UNIX, Microsoft Windows
              </p>
              <p>
               EXCEL
               <br/>
              </p>
              <p>
               Excel 2000 or 2002 spreadsheet
               <br/>
              </p>
              <p>
               .xls
               <br/>
              </p>
              <p>
               Microsoft Windows *
              </p>
              <p>
               EXCEL4
               <br/>
              </p>
              <p>
               Excel 4.0 spreadsheet
               <br/>
              </p>
              <p>
               .xls
               <br/>
              </p>
              <p>
               Microsoft Windows
              </p>
              <p>
               EXCEL5
               <br/>
              </p>
              <p>
               Excel 5.0 or 7.0 (95) spreadsheet
               <br/>
              </p>
              <p>
               .xls
               <br/>
              </p>
              <p>
               Microsoft Windows
              </p>
              <p>
               EXCEL97
               <br/>
              </p>
              <p>
               Excel 97 or 7.0 (95) spreadsheet
               <br/>
              </p>
              <p>
               .xls
               <br/>
              </p>
              <p>
               Microsoft Windows *
              </p>
              <p>
               EXCEL2000
               <br/>
              </p>
              <p>
               Excel 2000 spreadsheet
               <br/>
              </p>
              <p>
               .xls
               <br/>
              </p>
              <p>
               Microsoft Windows *
              </p>
              <p>
               EXCELCS
               <br/>
              </p>
              <p>
               Excel spreadsheet
               <br/>
              </p>
              <p>
               .xls
               <br/>
              </p>
              <p>
               UNIX
              </p>
              <p>
               JMP
               <br/>
              </p>
              <p>
               JMP table
               <br/>
              </p>
              <p>
               .jmp
               <br/>
              </p>
              <p>
               UNIX, Microsoft Windows
              </p>
              <p>
               PCFS
               <br/>
              </p>
              <p>
               Files on PC server
               <br/>
              </p>
              <p>
               .*
               <br/>
              </p>
              <p>
               UNIX
              </p>
              <p>
               TAB
               <br/>
              </p>
              <p>
               delimited file (tab-delimited values)
               <br/>
              </p>
              <p>
               .txt
               <br/>
              </p>
              <p>
               OpenVMS Alpha, UNIX, Microsoft Windows
              </p>
              <p>
               WK1
               <br/>
              </p>
              <p>
               Lotus 1-2-3 Release 2 spreadsheet
               <br/>
              </p>
              <p>
               .wk1
               <br/>
              </p>
              <p>
               Microsoft Windows
              </p>
              <p>
               WK3
               <br/>
              </p>
              <p>
               Lotus 1-2-3 Release 3 spreadsheet
               <br/>
              </p>
              <p>
               .wk3
               <br/>
              </p>
              <p>
               Microsoft Windows
              </p>
              <p>
               WK4
               <br/>
              </p>
              <p>
               Lotus 1-2-3 Release 4 or 5 spreadsheet
               <br/>
              </p>
              <p>
               .wk4
               <br/>
              </p>
              <p>
               Microsoft Windows
              </p>
              <p>
               2 导入一个文件夹下的所有文件的数据。
              </p>
              <p>
               2.1下面的代码导入一个文件夹下的所有文件的数据，要使用本代码需注意几点：首先，这个文件夹下的数据文件必须是同一类型分隔的数据，比如例子中都是tab分隔的txt文件，当然也可以对本代码进行改进，例如中间的proc import的dbms改为excel，就可以导入excel文件了。其次，本代码直接将文件名作为SAS数据集的名字，因此文件名必须是英文，且满足SAS命名规则。
              </p>
              <p>
               %macro directory(dir=);
              </p>
              <p>
               %let rs=%sysfunc(filename(filref,&amp;dir));
              </p>
              <p>
               %let did=%sysfunc(dopen(&amp;filref));
              </p>
              <p>
               %let nobs=%sysfunc(dnum(&amp;did));
              </p>
              <p>
               %do i=1 %to &amp;nobs.;
              </p>
              <p>
               %let name=%qscan(%qsysfunc(dread(&amp;did,&amp;i)),1,.);
              </p>
              <p>
               %let ext=%qscan(%qsysfunc(dread(&amp;did,&amp;i)),-1,.);
              </p>
              <p>
               proc import out=&amp;name. datafile="&amp;dir.\&amp;name..&amp;ext" dbms=tab replace;
              </p>
              <p>
               getnames=no;
              </p>
              <p>
               datarow=1;
              </p>
              <p>
               run;
              </p>
              <p>
               %end;
              </p>
              <p>
               %let rc=%sysfunc(dclose(&amp;did));
              </p>
              <p>
               %mend;
              </p>
              <p>
               %directory(dir=C:\PRIVATE);
              </p>
              <p>
               如果要将数据集进行汇总到一张表，或者则可以直接将proc import out=&amp;name中的&amp;name改为a&amp;i，然后对所有的a:数据集进行set操作。除此之外，我们还可以对&amp;ext进行设置来达到读取指定文件格式的数据。
              </p>
              <p>
               2.2 这里运用pipe读取到文件名称，再读取数据。首先建立三个数据集：
              </p>
              <p>
               data _null_;
              </p>
              <p>
               file 'c:\junk\extfile1.txt';
              </p>
              <p>
               put "05JAN2001 6 W12301 1.59 9.54";
              </p>
              <p>
               put "12JAN2001 3 P01219 2.99 8.97";
              </p>
              <p>
               run;
              </p>
              <p>
               data _null_;
              </p>
              <p>
               file 'c:\junk\extfile2.txt';
              </p>
              <p>
               put "02FEB2001 1 P01219 2.99 2.99";
              </p>
              <p>
               put "05FEB2001 3 A00901 1.99 5.97";
              </p>
              <p>
               put "07FEB2001 2 C21135 3.00 6.00";
              </p>
              <p>
               run;
              </p>
              <p>
               data _null_;
              </p>
              <p>
               file 'c:\junk\extfile3.txt';
              </p>
              <p>
               put "06MAR2001 4 A00101 3.59 14.36";
              </p>
              <p>
               put "12MAR2001 2 P01219 2.99 5.98";
              </p>
              <p>
               run;
              </p>
              <p>
               filename blah pipe 'dir C:\Junk /b';
              </p>
              <p>
               data _null_;
              </p>
              <p>
               infile blah truncover end=last;
              </p>
              <p>
               length fname $20;
              </p>
              <p>
               input fname;
              </p>
              <p>
               i+1;
              </p>
              <p>
               call symput('fname'||trim(left(put(i,8.))),scan(trim(fname),1,'.'));
              </p>
              <p>
               call symput('pext'||trim(left(put(i,8.))),trim(fname));
              </p>
              <p>
               if last then call symput('total',trim(left(put(i,8.))));
              </p>
              <p>
               run;
              </p>
              <p>
               %macro test;
              </p>
              <p>
               %do i=1 %to &amp;total;
              </p>
              <p>
               proc import datafile="c:\Junk\&amp;&amp;pext&amp;i"
              </p>
              <p>
               out=work.&amp;&amp;fname&amp;i
              </p>
              <p>
               dbms=dlm replace;
              </p>
              <p>
               delimiter=' ';
              </p>
              <p>
               getnames=no ;
              </p>
              <p>
               run;
              </p>
              <p>
               proc print data=work.&amp;&amp;fname&amp;i;;
              </p>
              <p>
               title &amp;&amp;fname&amp;i;
              </p>
              <p>
               run;
              </p>
              <p>
               %end;
              </p>
              <p>
               %mend;
              </p>
              <p>
               %test;
              </p>
              <p>
               这里，如果要导入指定文件类型的数据，例如txt，则只需要将filename blah pipe 'dir C:\Junk /b';改为filename blah pipe 'dir C:\Junk.*.txt /b';即可。
              </p>
              <p>
               除了用filename blah pipe 'dir C:\Junk.*.txt /b';得到指定类型的文件名，我们还可以%sysexec dir *.xls /b/o:n &gt; flist.txt;来将xls文件输出到指定的文件中，供读取操作用。这个将在下面的内容作介绍。
              </p>
              <p>
               3 导入excel表中的所有sheet的数据，并将其汇总到一个数据表中。
              </p>
              <p>
               3.1 Excel表是sas导入导出最多的数据表之一，本例中，我们将导入一个excel中的不同的数据
              </p>
              <p>
               %let dir=C:\ExcelFiles;
              </p>
              <p>
               %macro ReadXls (inf);
              </p>
              <p>
               libname excellib excel "&amp;dir.\&amp;inf";
              </p>
              <p>
               proc sql noprint;
              </p>
              <p>
               create table sheetname as
              </p>
              <p>
               select tranwrd(memname, "''", "'") as sheetname
              </p>
              <p>
               from sashelp.vstabvw
              </p>
              <p>
               where libname="EXCELLIB";
              </p>
              <p>
               select count(DISTINCT sheetname) into :cnt_sht
              </p>
              <p>
               from sheetname;
              </p>
              <p>
               select DISTINCT sheetname into :sheet1 – :sheet%left(&amp;cnt_sht)
              </p>
              <p>
               from sheetname;
              </p>
              <p>
               quit;
              </p>
              <p>
               libname excellib clear;
              </p>
              <p>
               %do i=1 %to &amp;cnt_sht;
              </p>
              <p>
               proc import datafile="&amp;dir.\&amp;inf"
              </p>
              <p>
               out=sheet&amp;i replace;
              </p>
              <p>
               sheet="&amp;&amp;sheet&amp;i";
              </p>
              <p>
               getnames=yes;
              </p>
              <p>
               mixed=yes;
              </p>
              <p>
               run;
              </p>
              <p>
               proc append base=master data=sheet&amp;i force;
              </p>
              <p>
               run;
              </p>
              <p>
               %end;
              </p>
              <p>
               %mend ReadXls;
              </p>
              <p>
               %ReadXls(all1.xls);
              </p>
              <p>
               这样，我们可以通过%ReadXls(all2.xls); %ReadXls(all3.xls);等来得到多个excel文件的所有数据集。
              </p>
              <p>
               3.2 我们可以结合3.1和2.1或2.2的方法来读取多个文件中的多个表。这里再介绍一种新的读取多个文件的方法：
              </p>
              <p>
               options noxwait;
              </p>
              <p>
               %macro ReadXls (dir=);
              </p>
              <p>
               %sysexec cd &amp;dir; %sysexec dir *.xls /b/o:n &gt; flist.txt;
              </p>
              <p>
               data _indexfile;
              </p>
              <p>
               length filen $200;
              </p>
              <p>
               infile "&amp;dir./flist.txt";
              </p>
              <p>
               input filen $;
              </p>
              <p>
               run;
              </p>
              <p>
               proc sql noprint;
              </p>
              <p>
               select count(filen) into :cntfile from _indexfile;
              </p>
              <p>
               %if &amp;cntfile&gt;=1 %then %do;
              </p>
              <p>
               select filen into :filen1-:filen%left(&amp;cntfile)
              </p>
              <p>
               from _indexfile;
              </p>
              <p>
               %end;
              </p>
              <p>
               quit;
              </p>
              <p>
               %do i=1 %to &amp;cntfile;
              </p>
              <p>
               libname excellib excel "&amp;dir.\&amp;&amp;filen&amp;i";
              </p>
              <p>
               proc sql noprint;
              </p>
              <p>
               create table sheetname as
              </p>
              <p>
               select tranwrd(memname, "''", "'") as sheetname
              </p>
              <p>
               from sashelp.vstabvw
              </p>
              <p>
               where libname="EXCELLIB";
              </p>
              <p>
               select count(DISTINCT sheetname) into :cnt_sht
              </p>
              <p>
               from sheetname;
              </p>
              <p>
               select DISTINCT sheetname into :sheet1 – :sheet%left(&amp;cnt_sht)
              </p>
              <p>
               from sheetname;
              </p>
              <p>
               quit;
              </p>
              <p>
               %do j=1 %to &amp;cnt_sht;
              </p>
              <p>
               proc import datafile="&amp;dir.\&amp;&amp;filen&amp;i"
              </p>
              <p>
               out=sheet&amp;j replace;
              </p>
              <p>
               sheet="&amp;&amp;sheet&amp;j";
              </p>
              <p>
               getnames=yes;
              </p>
              <p>
               mixed=yes;
              </p>
              <p>
               run;
              </p>
              <p>
               data sheet&amp;j;
              </p>
              <p>
               length _excelfilename $100 _sheetname $32;
              </p>
              <p>
               set sheet&amp;j;
              </p>
              <p>
               _excelfilename="&amp;&amp;filen&amp;z";
              </p>
              <p>
               _sheetname="&amp;&amp;sheet&amp;j";
              </p>
              <p>
               run;
              </p>
              <p>
               proc append base=master data=sheet&amp;j force;
              </p>
              <p>
               run;
              </p>
              <p>
               %end;
              </p>
              <p>
               libname excellib clear;
              </p>
              <p>
               %end;
              </p>
              <p>
               %mend ReadXls;
              </p>
              <p>
               %readxls (dir=C:\ExcelFiles);
              </p>
              <p>
               4 从多个文件夹下读取多个数据。
              </p>
              <p>
               直接给源代码吧。
              </p>
              <p>
               %macro etl(ds, ds2,path);
              </p>
              <p>
               data &amp;ds &amp;ds2;
              </p>
              <p>
               LENGTH DateTime 8
              </p>
              <p>
               UserName $ 20
              </p>
              <p>
               Submit $ 10
              </p>
              <p>
               SentNumber $ 11
              </p>
              <p>
               IP $ 15
              </p>
              <p>
               MessageID $ 15
              </p>
              <p>
               SendingMode $ 6
              </p>
              <p>
               Contents $ 160 ;
              </p>
              <p>
               %let filrf=mydir;
              </p>
              <p>
               %let rc=%sysfunc(filename(filrf,"&amp;path"));
              </p>
              <p>
               %let did=%sysfunc(dopen(&amp;filrf));
              </p>
              <p>
               %let memcount=%sysfunc(dnum(&amp;did));
              </p>
              <p>
               %do i=1 %to &amp;memcount;
              </p>
              <p>
               AccountNum+1;
              </p>
              <p>
               %let counter = AccountNum;
              </p>
              <p>
               %let username&amp;i=%sysfunc(dread(&amp;did,&amp;i));
              </p>
              <p>
               %let filref=mydir2;
              </p>
              <p>
               %let file=%sysfunc(filename(filref,"&amp;path\&amp;&amp;username&amp;i"));
              </p>
              <p>
               %let op=%sysfunc(dopen(&amp;filref));
              </p>
              <p>
               %let flcount=%sysfunc(dnum(&amp;op));
              </p>
              <p>
               filename FT77F001 "D:\SMSGatewayData2\USERS\&amp;&amp;username&amp;i\*.log";
              </p>
              <p>
               %do j=1 %to &amp;flcount;
              </p>
              <p>
               %let trans&amp;j=%sysfunc(dread(&amp;op,&amp;j));
              </p>
              <p>
               %put '&amp;&amp;username&amp;i = ' &amp;&amp;username&amp;i '&amp;&amp;trans&amp;j= ' &amp;&amp;trans&amp;j '&amp;flcount = ' &amp;flcount '&amp;filref = ' &amp;filref '&amp;filrf = ' &amp;filrf;
              </p>
              <p>
               infile FT77F001 filename=filename eov=eov end = done length=L DSD;
              </p>
              <p>
               INPUT DateTime : ANYDTDTM19.
              </p>
              <p>
               UserName $
              </p>
              <p>
               Submit $
              </p>
              <p>
               SentNumber $
              </p>
              <p>
               IP $
              </p>
              <p>
               MessageID $
              </p>
              <p>
               SendingMode $
              </p>
              <p>
               Contents $;
              </p>
              <p>
               output;
              </p>
              <p>
               %end;
              </p>
              <p>
               %end;
              </p>
              <p>
               run;
              </p>
              <p>
               %mend;
              </p>
              <p>
               %etl(sms2, sms,D:\SMSGatewayData2\USERS)
              </p>
              <p>
               这里就不做测试了，原理其实跟3.2差不多，就是将目录也参数化。
              </p>
              <p>
               5 参考文献：
              </p>
              <p>
               A Case Study of Importing Multiple Worksheet Files
              </p>
              <p>
               <a class="d4pbbc-url" href="http://www2.sas.com/proceedings/sugi31/034-31.pdf" rel="nofollow" target="_blank">
                http://www2.sas.com/proceedings/sugi31/034-31.pdf
               </a>
              </p>
              <p>
               Reading multiple files with PROC IMPORT
              </p>
              <p>
               <a class="d4pbbc-url" href="http://support.sas.com/kb/24/707.html" rel="nofollow" target="_blank">
                http://support.sas.com/kb/24/707.html
               </a>
              </p>
              <p>
               How to Invoke PROC IMPORT to read a CSV, TAB or Delimited File
              </p>
              <p>
               <a class="d4pbbc-url" href="http://ftp.sas.com/techsup/download/sample/datastep/import.html" rel="nofollow" target="_blank">
                http://ftp.sas.com/techsup/download/sample/datastep/import.html
               </a>
              </p>
              <p>
               Use PROC IMPORT to read a CSV, TAB or delimited file.sas
              </p>
              <p>
               Reading multiple files with PROC IMPORT.sas
              </p>
              <p>
               directory_import_dbms.sas
              </p>
              <p>
               proc_import_dbf.html
              </p>
              <p>
               <a class="d4pbbc-url" href="http://ftp.sas.com/" rel="nofollow" target="_blank">
                http://ftp.sas.com/
               </a>
              </p>
             </div>
             <!-- .bbp-reply-content -->
            </div>
            <!-- .reply -->
            <div class="bbp-reply-header" id="post-277542">
             <div class="bbp-meta">
              <span class="bbp-reply-post-date">
               2009年9月17日 下午2:32
              </span>
              <a class="bbp-reply-permalink" href="http://cos.name/cn/topic/16507/#post-277542">
               2 楼
              </a>
              <span class="bbp-admin-links">
              </span>
             </div>
             <!-- .bbp-meta -->
            </div>
            <!-- #post-277542 -->
            <div class="even bbp-parent-forum-998 bbp-parent-topic-16507 bbp-reply-position-2 user-id-2172 post-277542 reply type-reply status-publish hentry">
             <div class="bbp-reply-author">
              <a class="bbp-author-avatar" href="http://cos.name/cn/profile/2172/" rel="nofollow" title="查看kaikaiok8的档案">
               <img src="http://sdn.geekzu.org/avatar/09bb0e1175bb28e69acfd177f66c5394?s=80&amp;d=monsterid&amp;r=g"/>
              </a>
              <br/>
              <a class="bbp-author-name" href="http://cos.name/cn/profile/2172/" rel="nofollow" title="查看kaikaiok8的档案">
               kaikaiok8
              </a>
              <br/>
              <div class="bbp-author-role">
               普通会员
              </div>
             </div>
             <!-- .bbp-reply-author -->
             <div class="bbp-reply-content">
              <p>
               beautiful.
              </p>
             </div>
             <!-- .bbp-reply-content -->
            </div>
            <!-- .reply -->
           </li>
           <!-- .bbp-body -->
           <li class="bbp-footer">
            <div class="bbp-reply-author">
             作者
            </div>
            <div class="bbp-reply-content">
             帖子
            </div>
            <!-- .bbp-reply-content -->
           </li>
           <!-- .bbp-footer -->
          </ul>
          <!-- #topic-16507-replies -->
          <div class="bbp-pagination">
           <div class="bbp-pagination-count">
            查看 2 个帖子 - 1 到 2（总计 2 个）
           </div>
           <div class="bbp-pagination-links">
           </div>
          </div>
          <div class="bbp-no-reply" id="no-reply-16507">
           <div class="bbp-template-notice">
            <p>
             您必须先登录才能回复该主题。
            </p>
           </div>
          </div>
         </div>
        </div>
        <!-- .entry-content -->
        <footer class="entry-meta">
        </footer>
        <!-- .entry-meta -->
       </article>
       <!-- #post -->
       <div class="comments-area" id="comments">
       </div>
       <!-- #comments -->
      </div>
      <!-- #content -->
     </div>
     <!-- #primary -->
     <div class="sidebar-container" id="tertiary" role="complementary">
      <div class="sidebar-inner">
       <div class="widget-area">
        <aside class="widget bbp_widget_login" id="bbp_login_widget-2">
         <h3 class="widget-title">
          登录
         </h3>
         <form action="http://cos.name/cn/wp-login.php" class="bbp-login-form" method="post">
          <fieldset>
           <legend>
            登录
           </legend>
           <div class="bbp-username">
            <label for="user_login">
             用户名:
            </label>
           </div>
           <div class="bbp-password">
            <label for="user_pass">
             密码:
            </label>
           </div>
           <div class="bbp-remember-me">
            <label for="rememberme">
             记住用户名
            </label>
           </div>
           <div class="bbp-submit-wrapper">
            <button class="button submit user-submit" id="user-submit" name="user-submit" tabindex="104" type="submit">
             登录
            </button>
           </div>
           <div class="bbp-login-links">
            <a class="bbp-register-link" href="http://cos.name/cn/wp-login.php?action=register" title="注册">
             注册
            </a>
            <a class="bbp-lostpass-link" href="http://cos.name/cn/wp-login.php?action=lostpassword" title="忘记密码">
             忘记密码
            </a>
           </div>
          </fieldset>
         </form>
        </aside>
        <aside class="widget widget_text" id="text-7">
         <h3 class="widget-title">
          搜索
         </h3>
         <div class="textwidget">
          <form action="http://www.google.com/search" id="bbp-search-form" method="get" onsubmit="Gsitesearch(this)" role="search">
           <div>
           </div>
          </form>
          <form id="bbp-search-form-baidu" onsubmit="g(this)" role="search">
           <div>
           </div>
          </form>
         </div>
        </aside>
        <aside class="widget widget_text" id="text-2">
         <h3 class="widget-title">
          新鲜事
         </h3>
         <div class="textwidget">
          <ul>
           <li>
            <a href="http://cos.name/cn/topics/">
             最新帖子
            </a>
           </li>
           <li>
            <a href="http://cos.name/cn/view/popular/">
             最热门主题
            </a>
           </li>
           <li>
            <a href="http://cos.name/cn/view/no-replies/">
             消灭零回复
            </a>
           </li>
          </ul>
         </div>
        </aside>
        <aside class="widget widget_text" id="text-3">
         <h3 class="widget-title">
          RSS订阅
         </h3>
         <div class="textwidget">
          <ul>
           <li>
            <img src="http://cos.name/wp-includes/images/rss.png"/>
            <a href="http://cos.name/cn/topics/feed/">
             所有主题
            </a>
           </li>
           <li>
            <img src="http://cos.name/wp-includes/images/rss.png"/>
            <a href="http://cos.name/cn/forums/feed/">
             所有帖子
            </a>
           </li>
          </ul>
         </div>
        </aside>
       </div>
       <!-- .widget-area -->
      </div>
      <!-- .sidebar-inner -->
     </div>
     <!-- #tertiary -->
    </div>
    <!-- #main -->
    <footer class="site-footer" id="colophon" role="contentinfo">
     <div class="site-info">
      版权所有 © 2014 统计之都 | 由
      <a href="http://wordpress.org/">
       WordPress
      </a>
      构建 | 主题修改自
      <a href="http://wordpress.org/themes/twentythirteen">
       Twenty Thirteen
      </a>
     </div>
     <!-- .site-info -->
    </footer>
    <!-- #colophon -->
   </div>
   <!-- #page -->
  </div>
 </body>
</html>